package com.itheima.health.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.ReportService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.SetmealReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.log4j.Log4j2;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author zhiHaoD
 * @since 2022/11/23 10:40
import java.sql.Array;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author ShiXTong
 * @since 2022/11/22 23:11
 */
@RequestMapping("/report")
@RestController
@Log4j2
public class ReportController {
    @Autowired
    private ReportService reportService;

    @Autowired
    private SetMealService setMealService;

    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        try {
            Map<String, Object> data = reportService.getBusinessReportData();
            return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, data);
        } catch (Exception e) {
            return new Result(false, MessageConst.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @GetMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> data = reportService.getBusinessReportData();
            String reportDate = (String) data.get("reportDate");
            Integer todayNewMember = (Integer) data.get("todayNewMember");
            Integer totalMember = (Integer) data.get("totalMember");
            Integer thisWeekNewMember = (Integer) data.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) data.get("thisMonthNewMember");
            Integer todayOrderNumber = (Integer) data.get("todayOrderNumber");
            Integer todayVisitsNumber = (Integer) data.get("todayVisitsNumber");
            Integer thisWeekOrderNumber = (Integer) data.get("thisWeekOrderNumber");
            Integer thisWeekVisitsNumber = (Integer) data.get("thisWeekVisitsNumber");
            Integer thisMonthOrderNumber = (Integer) data.get("thisMonthOrderNumber");
            Integer thisMonthVisitsNumber = (Integer) data.get("thisMonthVisitsNumber");
            List<Map> hotSetmeal = (List<Map>) data.get("hotSetmeal");
//            String path = request.getSession().getServletContext().getRealPath("vo") + File.separator + "report_template.xlsx";
            XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("D:\\Develop\\workspacee\\health2\\src\\main\\resources\\report_template.xlsx")));
            XSSFSheet sheetAt = excel.getSheetAt(0);

            XSSFRow row = sheetAt.getRow(2);
            row.getCell(5).setCellValue(reportDate);//日期

            row = sheetAt.getRow(4);
            row.getCell(5).setCellValue(todayNewMember);//新增会员
            row.getCell(7).setCellValue(totalMember);//总会员

            row = sheetAt.getRow(5);
            row.getCell(5).setCellValue(thisWeekNewMember);//本周新增会员
            row.getCell(7).setCellValue(thisMonthNewMember);//本月新增会员

            row = sheetAt.getRow(7);
            row.getCell(5).setCellValue(todayOrderNumber);//今日预约
            row.getCell(7).setCellValue(todayVisitsNumber);//今日到诊

            row = sheetAt.getRow(8);
            row.getCell(5).setCellValue(thisWeekOrderNumber);//本周预约
            row.getCell(7).setCellValue(thisWeekVisitsNumber);//本周到诊

            row = sheetAt.getRow(9);
            row.getCell(5).setCellValue(thisMonthOrderNumber);//本月预约
            row.getCell(7).setCellValue(thisMonthVisitsNumber);//本月到诊

            int rowNum = 12;
            for (Map map : hotSetmeal) {
                String name = (String) map.get("name");
                Long setmeal_count = (Long) map.get("setmeal_count");
                BigDecimal proportion = (BigDecimal) map.get("proportion");
                row = sheetAt.getRow(rowNum++);
                row.getCell(4).setCellValue(name);//名称
                row.getCell(5).setCellValue(setmeal_count);//预约数量
                row.getCell(6).setCellValue(proportion.doubleValue());//占比
            }
            OutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表的是excel文件类型
            response.setHeader("content-Disposition,=", "attachment;filename=report.xlsx");//指定以附件形式下载
            excel.write(out);
            out.flush();
            out.close();
            excel.close();
            return null;
        } catch (Exception e) {
            log.info("异常信息：{}", e.getLocalizedMessage());
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        log.info("套餐占比情况===== >");
        //查询套餐预约的数量
        List<SetmealReportVo> list = reportService.findSetmealCount();
        log.info("套餐信息列表: " + JSONObject.toJSONString(list));

        Map<String,Object> map = new HashMap<>();
        map.put("setmealCount",list);

        List<String> setMealNames = new ArrayList<>();
        for (SetmealReportVo setmealReportVo : list) {
            String name = setmealReportVo.getName();
            setMealNames.add(name);
        }
        map.put("setmealNames",setMealNames);
        // 返回结果
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
    }

    /**
     * 会员数量折线图
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        log.info("[统计-会员数量]");
        Map<String, Object> memberReportMap = reportService.getMemberReport();
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, memberReportMap);
    }

}
